package com.right.health.app.ui.main.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.magic.basic.utils.ToastUtil;
import com.magic.basic.widget.refresh.OnLastItemVisibleListener;
import com.magic.basic.widget.refresh.SwipeListRefreshLayout;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.app.App;
import com.right.health.app.base.BasePresenterFragment;
import com.right.health.app.bean.FamilyResponse;
import com.right.health.app.bean.MedicalRecordInfo;
import com.right.health.app.bean.MedicalResponse;
import com.right.health.app.constants.BroadCastKey;
import com.right.health.app.net.MedicalApi;
import com.right.health.app.ui.main.adapter.MedicalAdapter;
import com.right.health.app.ui.me.FamilyActivity;
import com.right.health.app.ui.medical.MedicalRecordDetailActivity;
import com.right.health.app.ui.medical.MedicalRecordAddActivity;
import com.right.health.app.ui.medical.MedicalTypeActivity;

import java.util.ArrayList;
import java.util.List;

import static com.right.health.app.constants.BroadCastCode.REFRESH_MEDICAL;

/*
 * Created by hopeliao on 2019-09-23
 * 医疗
 *
 */
public class MedicalFragment extends BasePresenterFragment implements View.OnClickListener, OnLastItemVisibleListener,
        SwipeRefreshLayout.OnRefreshListener, OnHttpListener {

    private static final int HTTP_CODE_REFRESH = 1;
    private static final int HTTP_CODE_LOAD_MORE = 2;

    private static final int REQUEST_CODE_FAMILY = 1;

    private MedicalAdapter mAdapter;

    private SwipeListRefreshLayout mRefreshView;

    private List<MedicalRecordInfo> mDataSource = new ArrayList<>();

    private int mPageNo;

    private TextView mUserTv;

    private MedicalApi mMedicalApi;

    @Override
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle, String s) {
        return layoutInflater.inflate(R.layout.medical, viewGroup, false);
    }

    @Override
    public void findView() {
        mUserTv = (TextView) findViewById(R.id.tv_user);
        mUserTv.setOnClickListener(this);

        mUserTv.setText(App.getInstance().getCurrentMember().name);

        mRefreshView = (SwipeListRefreshLayout) findViewById(R.id.refresh_view);
        mRefreshView.setLastItemVisibleListener(this);
        mRefreshView.setOnRefreshListener(this);

        findViewById(R.id.ib_add).setOnClickListener(this);
        findViewById(R.id.tv_medical_history).setOnClickListener(this);
        findViewById(R.id.tv_refresh_data).setOnClickListener(this);
        findViewById(R.id.ib_member).setOnClickListener(this);

        mAdapter = new MedicalAdapter(getActivity());
        ListView listView = (ListView) findViewById(R.id.listview);
        listView.setAdapter(mAdapter);
        listView.setOnItemClickListener((parent, view, position, id) -> {
//            MedicalRecordDetailActivity.launch(getActivity(),mDataSource.get(position).casehistoryid);
            Bundle bundle = new Bundle();
            bundle.putBoolean("isDetail",true);
            bundle.putString("idKey", mDataSource.get(position).casehistoryid);
            Intent intent= new Intent(getActivity(), MedicalRecordAddActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);
        });

    }

    @Override
    public void initialize() {
        mMedicalApi = new MedicalApi();
        mMedicalApi.setListener(this);

        onReceiveBroadcast(REFRESH_MEDICAL, null);
    }

    @Override
    public void onClick(View v) {
        if(!isAdded()) {
            return;
        }
        switch (v.getId()) {
            case R.id.ib_add: //添加病例
                startIntent(MedicalRecordAddActivity.class);
                break;
            case R.id.tv_medical_history: //病史
                startIntent(MedicalTypeActivity.class);
                break;
            case R.id.tv_user: //切换患者
            case R.id.ib_member: //成员管理
                FamilyActivity.makeActivity(this, true, REQUEST_CODE_FAMILY);
                break;
            case R.id.tv_refresh_data: //刷新
                onReceiveBroadcast(REFRESH_MEDICAL, null);
                break;
        }
    }

    @Override
    public void onReceiveBroadcast(int intent, Bundle bundle) {
        super.onReceiveBroadcast(intent, bundle);
        if(intent == REFRESH_MEDICAL) {
            mRefreshView.setRefreshing(true);
            onRefresh();
            mUserTv.setText(App.getInstance().getCurrentMember().name);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
         /*   if (requestCode == REQUEST_CODE_FAMILY) {
                Bundle bundle = data.getExtras();
                if (bundle != null) {
                    FamilyResponse.FamilyInfo familyInfo = bundle.getParcelable(BroadCastKey.DATA);
                    if (familyInfo != null ) {
                        mUserTv.setText(familyInfo.membername);

                        onReceiveBroadcast(REFRESH_MEDICAL, null);
                    }
                }
            }*/
        }
    }

    @Override
    public void onRefresh() {
        mPageNo = 1;
        mMedicalApi.addRequestCode(HTTP_CODE_REFRESH);
        mMedicalApi.getMedicalList(mPageNo, App.getInstance().getCurrentMember().id);

    }

    @Override
    public void onLastItemVisible() {
        mPageNo++;
        mMedicalApi.addRequestCode(HTTP_CODE_LOAD_MORE);
        mMedicalApi.getMedicalList(mPageNo, App.getInstance().getCurrentMember().id);
    }

    @Override
    public void onResponse(Request request, Response response) {
        if(!isAdded()) {
            return;
        }
        mRefreshView.onRefreshComplete();

        if(response.isResponseFail()) {
            ToastUtil.show(getActivity(), response.error);
        } else {
            if(response.targetData instanceof MedicalResponse) {
                MedicalResponse medicalResponse = response.cast(MedicalResponse.class);

                if(request.requestCode == HTTP_CODE_REFRESH) {
                    mDataSource.clear();
                }
                mDataSource.addAll(medicalResponse.casehistorys);

                if(medicalResponse.page != null) {
                    mRefreshView.setCanLoadMore(!medicalResponse.page.lastPage);
                }


                mAdapter.setDataSource(mDataSource);
            }
        }
    }
}
